package com.jrwd.recycletest.mange;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;

/**
 * 相信自己，创造未来
 *
 * @author 小兵
 * @date 2017/10/20
 */

public class ActivitiesManager {

    /**
     * activity集合
     */
    private static Stack<Activity> activities = new Stack<>();

    /**
     * 创建activity
     * @param activity
     */
    public static void createActivity(Activity activity){
        if(activity ==null){
            return;
        }
        activities.add(activity);
    }

    /**
     * 移除当前activity
     * @param activity
     */
    public static void removeActivity(Activity activity){
        if(activity ==null){
            return;
        }
        activities.remove(activity);
    }

    /**
     * 关闭所有的activity
     */
    public static void closeAll(){
        Iterator<Activity> iterator =  activities.iterator();
        while(iterator.hasNext()){
            Activity activity = iterator.next();
            activity.finish();
            iterator.remove();
        }
    }

    /**
     * 关闭当前的 activity
     * @param activity
     */
    public  static void  closeThis(Activity activity){
        if(activity ==null){
            return;
        }
        Iterator<Activity> iterator =  activities.iterator();
        while(iterator.hasNext()){
            Activity a = iterator.next();
            if(a==activity){
                a.finish();
                iterator.remove();
            }
        }
    }

    /**
     * 关闭除了当前这一个activity 的其他activity
     * @param activity
     */
    public static void closeExcept(Activity activity){
        if(activity ==null){
            return;
        }
        Iterator<Activity> iterator =  activities.iterator();
        while(iterator.hasNext()){
            Activity a = iterator.next();
            if(a!=activity){
                a.finish();
                iterator.remove();
            }
        }
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if (activities.isEmpty()) {
            return null;
        }
        Activity activity = activities.lastElement();
        return activity;
    }


    /**
     * 根据给定的class进行Aactivity跳转
     * @param activity            Activity对象
     * @param targetActivity      要跳转的Activity
     * @param isFinish            是否关闭Activity对象
     */
    public static void intentActivity(Activity activity, Class<? extends Activity> targetActivity, boolean isFinish){
        activity.startActivity(new Intent(activity, targetActivity));
        if(isFinish){
            activity.finish();
        }
    }

    /**
     * 根据给定的class进行Aactivity跳转
     * @param activity          Activity对象
     * @param targetActivity    要跳转的Activity
     * @param requestCode       申请的code
     */
    public static void intentActivity(Activity activity, Class<? extends Activity> targetActivity, int requestCode){
        activity.startActivityForResult(new Intent(activity, targetActivity), requestCode);
    }

    /**
     * 根据给定的Intent进行Activity跳转
     * @param activity          Activity对象
     * @param intent            要传递的Intent对象
     * @param isFinish          是否关闭Activity对象
     */
    public static void intentActivity(Activity activity, Intent intent, boolean isFinish){
        activity.startActivity(intent);
        if(isFinish){
            activity.finish();
        }
    }

    /**
     * 根据给定的Intent进行Activity跳转
     * @param activity          Activity对象
     * @param intent            要传递的Intent对象
     * @param requestCode       申请的code
     */
    public static void intentActivity(Activity activity, Intent intent, int requestCode){
        activity.startActivityForResult(intent, requestCode);

    }

    /**
     * 根据给定的class，并且带参数进行Aactivity跳转
     * @param activity            Activity对象
     * @param targetActivity      要跳转的Activity
     * @param params              传递的数据集合
     * @param isFinish            是否关闭Activity对象
     */
    public static void intentActivity(Activity activity, Class<? extends Activity> targetActivity,
                                      Map<String,Object> params, boolean isFinish){
        if( null != params ){

            Intent intent = new Intent(activity,targetActivity);
            if (!(activity instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            for(Map.Entry<String, Object> entry : params.entrySet()){
                setValueToIntent(intent, entry.getKey(), entry.getValue());
            }
            intentActivity(activity, intent, isFinish);
        }
    }

    /**
     * 根据给定的class，并且带参数进行Aactivity跳转
     * @param activity            Activity对象
     * @param targetActivity      要跳转的Activity
     * @param params              传递的数据集合
     * @param requestCode       申请的code
     */
    public static void intentActivity(Activity activity, Class<? extends Activity> targetActivity,
                                      Map<String,Object> params, int requestCode){
        if( null != params ){
            Intent intent = new Intent(activity,targetActivity);
            for(Map.Entry<String, Object> entry : params.entrySet()){
                setValueToIntent(intent, entry.getKey(), entry.getValue());
            }
            activity.startActivityForResult(intent, requestCode);
        }
    }

    /**
     * @param intent         Inent对象
     * @param key            Inent对象的key
     * @param val            Inent对象的值
     */
    public static void setValueToIntent(Intent intent, String key, Object val) {
        if (val instanceof Boolean) {
            intent.putExtra(key, (Boolean) val);
        } else if (val instanceof Boolean[]) {
            intent.putExtra(key, (Boolean[]) val);
        } else if (val instanceof String) {
            intent.putExtra(key, (String) val);
        } else if (val instanceof String[]) {
            intent.putExtra(key, (String[]) val);
        } else if (val instanceof Integer) {
            intent.putExtra(key, (Integer) val);
        } else if (val instanceof Integer[]) {
            intent.putExtra(key, (Integer[]) val);
        } else if (val instanceof Long) {
            intent.putExtra(key, (Long) val);
        } else if (val instanceof Long[]) {
            intent.putExtra(key, (Long[]) val);
        } else if (val instanceof Double) {
            intent.putExtra(key, (Double) val);
        } else if (val instanceof Double[]) {
            intent.putExtra(key, (Double[]) val);
        } else if (val instanceof Float) {
            intent.putExtra(key, (Float) val);
        } else if (val instanceof Float[]) {
            intent.putExtra(key, (Float[]) val);
        }
    }

    /**
     * 开启服务
     * @param activity    activity对象
     * @param targetService      service对象
     * @param params             传递参数
     */
    public static void intentService(Context activity, Class<? extends Service> targetService,
                                     Map<String,Object> params){
        Intent intent = new Intent(activity,targetService);
        if( null != params ){
            for(Map.Entry<String, Object> entry : params.entrySet()){
                setValueToIntent(intent, entry.getKey(), entry.getValue());
            }
            activity.startService(intent);
        }else {
            activity.startService(intent);
        }
    }

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = context.getApplicationContext().getPackageName();
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null)
            return false;
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                //前台
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

}
